#include "../include/common.h"

#ifdef WIN32
#pragma warning( push )
#pragma warning( disable : 4996)
#include <Windows.h>
#endif

namespace utility
{
namespace system
{
const char * get_exe_path()
{
#ifdef WIN32
    static char szPath[MAX_PATH];
    static bool bFirstTime = true;

    if(bFirstTime)
    {
        bFirstTime = false;
        GetModuleFileName(NULL, szPath, sizeof(szPath));
        char *p = strrchr(szPath, '\\');
        *p = '\0';
    }

    return szPath;
#else
    return ".";
#endif
}

time_t str2time_t(const char * szTime)
{
    struct tm tm1;
    time_t result;
    if (strlen(szTime) < 11)
    {
        time_t now = time(NULL);
#ifdef WIN32
        localtime_s(&tm1, &now);
#else
        localtime_r(&now, &tm1);
#endif 
        sscanf(szTime, "%d:%d:%d", 
            &(tm1.tm_hour),
            &(tm1.tm_min),
            &(tm1.tm_sec));
    }
    else
    {
        sscanf(szTime,"%d-%d-%d %d:%d:%d" ,     
            &(tm1.tm_year), 
            &(tm1.tm_mon), 
            &(tm1.tm_mday),
            &(tm1.tm_hour),
            &(tm1.tm_min),
            &(tm1.tm_sec));    
        tm1.tm_year -= 1900;
        tm1.tm_mon --;
    }
    tm1.tm_isdst=-1;
    result = mktime(&tm1);
    return result;
}

}   //  namespace system
}   //  namespace utility

#ifdef WIN32
#pragma warning( pop ) 
#endif

